package com.dlws.modules.audio.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dlws.common.utils.Constants;
import com.dlws.common.utils.DateUtils;
import com.dlws.common.web.BaseController;
import com.dlws.modules.audio.entity.AudioInfo;
import com.dlws.modules.audio.entity.AudioInfoPageVo;
import com.dlws.modules.audio.service.AudioInfoService;
import com.dlws.modules.monitoring.entity.ComputerInfoPageVo;
import com.dlws.modules.monitoring.service.ComputerService;
import com.page.Pagination;

@Controller
@RequestMapping(value = "/audio")
public class AudioInfoController extends BaseController {

	@Autowired
	private AudioInfoService audioInfoService;
	@Autowired
	private ComputerService computerService;

	/**
	 * 
	 * 描述：进入信号源页面
	 * 创建人： 高磊
	 * 创建时间: 2016-6-2 上午11:13:55
	 * @version
	 */
	@RequestMapping(value = "/computerList")
	public String computerList(ModelMap model, Integer pageNo,
			HttpServletRequest request, String searchName, Integer searchType) {
		logger.info("--进入信号源列表方法--产品模糊查询--分页--");
		/*
		 * ComputerInfo computerInfo = new ComputerInfo();
		 * computerInfo.setComPid(0); List<ComputerInfo> queryAll =
		 * computerService.queryAll(computerInfo); map.put("list", queryAll);
		 */
		StringBuilder params = new StringBuilder();
		ComputerInfoPageVo computerInfo = new ComputerInfoPageVo();
		// 如果页号为null 或小于1 设为1
		computerInfo.setPageNo(Pagination.cpn(pageNo));
		computerInfo.setPageSize(Constants.PAGE_SIZE);
		computerInfo.setComPid(0);
		computerInfo.setIsDelete(0);
		computerInfo.setIsUse(0);
		Pagination pagination = computerService.pagelist(computerInfo);

		// 分页展示
		String url = request.getContextPath()
				+ "/audio/computerList.do";
		pagination.pageView(url, params.toString());
		model.put("page", pagination);
		return "/audio/list";

	}

	/**
	 * 
	 * 描述：查看详情
	 * 创建人： 高磊
	 * 创建时间: 2016-6-2 上午8:26:07
	 * @version
	 */
	@RequestMapping(value = "/computerListbyPid")
	public String computerListbyPid(ModelMap model, Integer pageNo,HttpServletRequest request,String id ) {
		logger.info("--进入信号源列表方法--产品模糊查询--分页--");
		/*ComputerInfo computerInfo = new ComputerInfo();
		computerInfo.setComPid(Integer.valueOf(id));
		List<ComputerInfo> queryAll = computerService.queryAll(computerInfo);
		map.put("list", queryAll);*/
		StringBuilder params = new StringBuilder();
		if(null!=id && StringUtils.isNotBlank(id)){
			params.append("id=").append(id);
		}
		ComputerInfoPageVo computerInfo = new ComputerInfoPageVo();
		// 如果页号为null 或小于1 设为1
		computerInfo.setPageNo(Pagination.cpn(pageNo));
		computerInfo.setPageSize(Constants.PAGE_SIZE);
		computerInfo.setComPid(Integer.valueOf(id));
		computerInfo.setIsDelete(0);
		computerInfo.setIsUse(0);
		Pagination pagination = computerService.pagelist(computerInfo);

		// 分页展示
		String url = request.getContextPath()
				+ "/audio/computerListbyPid.do";
		pagination.pageView(url, params.toString());
		model.put("page", pagination);
		return "/audio/qwlistbypid";
	}

	/**
	 * 
	 * 描述：进入添加信号源所在地页面
	 * 创建人： 高磊
	 * 创建时间: 2016-6-1 下午4:52:15
	 * @version
	 */

	@RequestMapping(value = "/addAudioInfo")
	public String addComputerInfo(String id, ModelMap model) {
		model.put("oid", id);
		return "/audio/addaudioinfo";

	}

	/**
	 * 
	 * 描述：添加信号源
	 * 创建人： 高磊
	 * 创建时间: 2016-6-2 上午11:46:10
	 * @version
	 */
	@RequestMapping(value = "/audioAdd")
	public String audioAdd(AudioInfo audioInfo, String audioName, String audioAddress) {
		audioInfo.setCreateTime(DateUtils.getCurrentDate());
		audioInfo.setAudioName(String.valueOf(audioName));
		audioInfo.setAudioAddress(String.valueOf(audioAddress));
		audioInfoService.addSelective(audioInfo);
		return "redirect:/audio/audioListbyOid.do?id=" + audioInfo.getComId();
	}

	/**
	 * 
	 * 描述：查看信号源
	 * 创建人： 高磊
	 * 创建时间: 2016-6-3 上午9:26:28
	 * @version
	 */
	@RequestMapping(value = "/audioListbyOid")
	public String AudioListByOid(ModelMap model, Integer pageNo,HttpServletRequest request,String id) {
		logger.info("--进入信号源方法--产品模糊查询--分页--");
		/*AudioInfo audioInfo = new AudioInfo();
		audioInfo.setComId(Integer.valueOf(id));
		List<AudioInfo> queryAll = audioInfoService.queryAll(audioInfo);
		map.put("list", queryAll);*/
		StringBuilder params = new StringBuilder();
		if(null!=id && StringUtils.isNotBlank(id)){
			params.append("id=").append(id);
		}
		
		AudioInfoPageVo audioInfo = new AudioInfoPageVo();
		// 如果页号为null 或小于1 设为1
		audioInfo.setPageNo(Pagination.cpn(pageNo));
		audioInfo.setPageSize(Constants.PAGE_SIZE);
		audioInfo.setComId(Integer.valueOf(id));
		audioInfo.setIsDelete(0);
		audioInfo.setIsUse(0);
		Pagination pagination = audioInfoService.pagelist(audioInfo);


		// 分页展示
		String url = request.getContextPath()
				+ "/audio/audioListbyOid.do";
		pagination.pageView(url, params.toString());
		model.put("page", pagination);
		return "/audio/audioselectlistbypid";

	}

	/**
	 * 
	 * 描述：删除
	 * 创建人： 高磊
	 * 创建时间: 2016-6-3 上午10:00:56
	 * @version
	 */
	@RequestMapping(value = "/audioDel")
	public String audioDel(AudioInfo audioInfo) {
		if (audioInfo.getIsDelete() == 0) {
			// 0删除 ，1不删除
			audioInfo.setIsDelete(1);
		} else {
			audioInfo.setIsDelete(0);
		}
		audioInfoService.updateByPrimaryKeySelective(audioInfo);
		return "redirect:/audio/audioListbyOid.do?id=" + audioInfo.getComId();

	}

	/**
	 * 
	 * 描述：更改启用
	 * 创建人： 高磊
	 * 创建时间: 2016-6-3 上午10:26:26
	 * @version
	 */
	@RequestMapping(value = "/audioUp")
	public String audioUp(AudioInfo audioInfo) {
		if (audioInfo.getIsUse() == 0) {
			// 0不启用 1启用
			audioInfo.setIsUse(1);
		} else {
			audioInfo.setIsUse(0);
		}
		audioInfoService.updateByPrimaryKeySelective(audioInfo);
		return "redirect:/audio/audioListbyOid.do?id=" + audioInfo.getComId();
	}
	/**
	 * 
	 * 描述：信号源选择
	 * 创建人： 高磊
	 * 创建时间: 2016-6-13 上午9:20:34
	 * @version
	 */
	@RequestMapping(value = "/audioViewList")
	public String audioViewList(){
		return "/audio/audioviewlist";
		
	}
}
